package config

import (
	"fmt"

	"github.com/meilihao/golib/v2/log"
)

var (
	propertiesMap = make(map[string]string)
)

// todo
type GlobalConfig struct {
	Name                       string
	Category                   string
	Description                string
	Type                       string
	ValidatorRegularExpression string
	DefaultValue               string
	Value                      string
	Linked                     bool

	// UpdateExtensions []

	// Evtf cloudbus.EventFacade
}

func NewGlobalConfig(category, name string) *GlobalConfig {
	return &GlobalConfig{
		Category: category,
		Name:     name,
	}
}

func (gc *GlobalConfig) GetClass() string {
	return "GlobalConfig"
}

func (gc *GlobalConfig) GetIdentity() string {
	return ""
}

func (gc *GlobalConfig) UniqueIdentity() string {
	return ""
}

func (gc *GlobalConfig) Init() string {
	gc.Evtf.OnEventCallback(fmt.Sprintf(UpdateEventPath2, gc.Category, gc.Name), gc)
	return ""
}

func (gc *GlobalConfig) Run(tokens map[string]string, T interface{}) {
	nodeUuid := tokens["nodeUuid"]
	if nodeUuid == "{ManagementServerId}" {
		return
	}

	// todo

	evt := T.(UpdateEvent)
	log.Glog.Info(fmt.Sprintf("GlobalConfig[category: %s, name: %s] was updated in other management node[uuid:%s],"+
		"in line with that change, updated ours. %s --> %s", gc.Category, gc.Name, nodeUuid, evt.OldValue, gc.Value))
}
